import data from '@/common/data.js'; //筛选菜单数据
import validator from '@/common/validator'
export default {
	data() {
		return {
			placeData: null,
			loadShow1: false,
			// 记录一共有多少条
			allSize: null,

			// 记录当前页数
			page: 1,
			// 记录总页数
			allPage: null,
			status: 'loadmore',
			defaultSelected: [],
			filterData: [],
			keyword: null,
			// 记录页数
			page: 1,
			pageSize: 10,
			// 公司的数量
			allCompany: [],
			chooseObj: {},
			// 判断字符a是否存在
			wordA: null,
			// 判断字符b是否存在
			wordB: null

		}
	},
	onLoad: function() {
		this.findAllConmpany()
		this.placeData = uni.getStorageSync('filterPlaceData')
		this.getPlaceData()
		//定时器模拟ajax异步请求数据
		this.filterData = []
		// console.log('修改菜单');
		let companyType = uni.getStorageSync('filterCompanyType')

		this.filterData.push({
			name: '地区',
			"type": 'hierarchy',
			"submenu": this.placeData
		})

		this.filterData.push({
			"name": '类型',
			"type": 'filter',
			"submenu": [{
				"name": "类型",
				"submenu": companyType
			}]
		})
		this.changeSelected()
	},
	onReachBottom() {
		// console.log("-----------", this.page, this.allPage)

	},
	methods: {
		getPlaceData() {
			let data = uni.getStorageSync('filterPlaceData')
			this.placeData = data
			this.areaList = []
			let obj = {
				name: '省份',
				value: '省份',
				submenu: [{
					name: "全部",
					value: "全部"
				}]
			}

			data.forEach(item => {
				obj.submenu.push({
					name: item.name,
					value: 'A' + item.name
				})
			})
			this.placeData.unshift(obj)
			this.areaList.push(obj)
		
		},

		// 
		refreshArr() {
			if (this.page >= this.allPage) {
				this.status = 'nomore';
				return
			}
			this.status = 'loading';
			this.page = ++this.page;
			setTimeout(() => {
				this.findAllConmpany()
				if (this.page >= this.allPage) this.status = 'nomore';
				else this.status = 'loading';
			}, 2000)
		},
		// 跳转至资质详情页面
		toDetail(item) {
			uni.navigateTo({
				url: `/pagesFind/detailenterprise/mainPerson/mainPerson?companyCid=${item.companyCid}&&index=4&&hide=0`
			})
		},
		// 搜索
		searchResult(value) {
			
			// 点击搜索  验证不为空后，调用接口
			if (!validator.checkNullValue(this.keyword)) {
				return;
			}
			this.page = 1
			this.allCompany = []
			this.findAllConmpany()
		},

		// 查询所有公司信息
		findAllConmpany() {
			this.loadShow1 = true
			let param = {
				'page': this.page
			}
			if (this.chooseObj.qualification) {
				param.qualification = this.chooseObj.qualification
			}
			if (this.chooseObj.companyProvince) {
				param.companyProvince = this.chooseObj.companyProvince
			}
			if (this.chooseObj.companyCity) {
				param.companyCity = this.chooseObj.companyCity
			}
			if (this.keyword) {
				param.companyName = this.keyword
			}
			this.status = 'loading'
			this.$app.request({
				url: this.$baseApp.api.dicData.findAll,
				data: param,
				method: 'GET',
				dataType: 'json',
				success: res => {
					if (res.code == 200) {
						this.allPage = res.data.totalPages
						this.allSize = res.data.totalElements
						if (this.allPage <= 1 || res.data.content.length < 10) {
							this.status = 'nomore'
						} else {
							this.status = 'loadmore'
						}
						res.data.content.forEach((item) => {
							item.companyName=validator.joinStr(item.companyName,this.keyword)
							item.companyIndustry=validator.joinStr(item.companyIndustry,this.keyword)
							this.allCompany.push(item)
						})

					} else {
						this.$refs.uToast.show({
							message: res.msg,
							type: 'success'
						})
					}
				},
				complete: res => {
					this.loadShow1 = false
					uni.stopPullDownRefresh();
				}
			});
		},


		//接收菜单结果
		confirm(e) {
			this.indexArr = e.index;
			this.valueArr = e.value;
			
			// console.log("this.valueArr", this.valueArr)
			this.chooseObj = {}

			this.wordA = this.valueArr[0][1].indexOf('A')
			this.wordB = this.valueArr[0][1].indexOf('B')

			if (this.valueArr[0][1] && this.valueArr[0][1] == 'B市辖区') {
				this.chooseObj.companyProvince = this.valueArr[0][0].slice(1)

			} else {
				if (this.wordA > -1) {
					this.chooseObj.companyProvince = this.valueArr[0][1].slice(1)
				}
				if (this.wordB > -1) {
					this.chooseObj.companyCity = this.valueArr[0][1].slice(1)
				}
				this.wordA = null
				this.wordB = null
			}

			this.valueArr[1][0].forEach(item => {
				if (this.chooseObj.qualification) {
					this.chooseObj.qualification += ',' + item.code
				} else {
					this.chooseObj.qualification = item.code
				}
			})

			

			this.allCompany = []
			this.findAllConmpany()
		},
		//修改选中项-示例
		changeSelected() {
			//设置选中项
			// 一下的注释是针对测试数据说明结构的意思，具体传入什么数据，要看你自己数据。如果data.js数据有修改，注意defaultSelected也要修改
			//传入defaultSelected的结构不能错，错了就报错运行异常。 不想选中的请传入null

			this.defaultSelected = [];
			this.$nextTick(() => {
				this.defaultSelected = [
					[0, 0], //第0个菜单选中 一级菜单的第6项，二级菜单的第0项，三级菜单的第1项

					[
						[0],
						[1],
						[1]
					] //单选菜单选中 第一个筛选的第0项，第二个筛选的第1项，第三个筛选的第1项
				];
			})
		},
	}
}
